{include file="common/head"}
<div style="padding: 16px;">
  <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
      <button class="layui-btn layui-btn-sm layui-bg-red" lay-event="delCheck">删除选中</button>
  </div>
  </script>
<script type="text/html" id="barDemo">
  <div class="layui-clear-space">
    <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="del">删除</a>
  </div>
</script>
{include file="common/bottom"}


<script>
  layui.use(['table', 'dropdown'], function () {
    var table = layui.table;
    var dropdown = layui.dropdown;
    var $ = layui.$
    let delObj = []
    // 创建渲染实例

    table.render({
      elem: '#test',
      url: '/admin/logs/list',
      toolbar: '#toolbarDemo',
      limit: 20,
      parseData: function (res) { // res 即为原始返回的数据
        return {
          "code": res.code, // 解析接口状态
          "msg": res.msg, // 解析提示文本
          "count": res.count, // 解析数据长度
          "data": res.data.data
        };
      },
      defaultToolbar: ['filter', 'exports', 'print', { // 右上角工具图标
        title: '提示',
        layEvent: 'LAYTABLE_TIPS',
        icon: 'layui-icon-tips',
        onClick: function (obj) { // 2.9.12+
          layer.alert('自定义工具栏图标按钮');
        }
      }],
      height: 'full',
      css: [
        '.layui-table-tool-temp{padding-right: 145px;}'
      ].join(''),
      cellMinWidth: 80,
      totalRow: true, // 开启合计行
      page: true,
      cols: [[
        { type: 'checkbox' },
        { field: 'id', width: 80, title: 'ID', sort: true },
        { field: 'title', width: 100, title: '请求名称' },
        {
          field: 'user_id', width: 80, title: '用户ID', templet: (d) => {
            if (d.user_id == '0') {
              return '<span class="layui-badge-rim">游客</span>'
            } else {
              return '<span class="layui-badge-rim">' + d.user_id + '</span>'
            }
          }
        },
        { field: 'app', width: 80, title: '模型' },
        { field: 'request_url', width: 300, title: '请求地址' },
        { field: 'controller', title: '控制器', width: 80 },
        { field: 'action', title: '方法', width: 80 },
        {
          field: 'method', title: '请求方式', width: 80, templet: (d) => {
            if (d.method == 'POST') {
              return '<span class="layui-badge layui-bg-blue">POST</span>'
            } else {
              return '<span class="layui-badge layui-bg-green">GET</span>'
            }
          }
        },
        { field: 'ip', title: '请求IP', width: 120 },
        { field: 'create_time', title: '请求时间', width: 120 },
        { title: '操作', width: 300, minWidth: 125, toolbar: '#barDemo' }
      ]],
      done: function () {
        var id = this.id;
        // 删除选中
      },
      error: function (res, msg) {
        console.log(res, msg)
      }
    });
    table.on('toolbar(test)', function (obj) {
      var id = obj.config.id;
      var checkStatus = table.checkStatus(id);
      var othis = lay(this);
      switch (obj.event) {
        case 'delCheck':
          var data = checkStatus.data;
          var delId = ''
          data.forEach(e => {
            delId += e.id + ','
          });
          layer.confirm('确定要删除选中的日志吗？', {
            btn: ['确定', '取消'],
          }, function () {
            $.post('/admin/logs/del', { id: delId }, (res) => {
              layer.msg('操作成功')
              table.reload('test')
            })
          })
          break
      };

    });

    table.on('colTool(test)', function (obj) {
      var event = obj.event;
      if (event === 'email-tips') {
        layer.alert(layui.util.escape(JSON.stringify(obj.col)), {
          title: '当前列属性配置项'
        });
      }
    });

    table.on('tool(test)', function (obj) { // 双击 toolDouble
      var data = obj.data; // 获得当前行数据
      if (obj.event === 'del') {
        $.ajax({
          url: '/admin/logs/del/id/' + data.id
          , dataType: 'json'
          , success: (res) => {
            if (res.code == 200) {
              layer.msg('删除成功', { icon: 6 })
              obj.del()
            } else {
              layer.msg('删除失败', { icon: 5 })
            }
          }
        })
      }
    });
    // 行单击事件
    table.on('row(test)', function (obj) {
      //layer.closeAll('tips');
    });
    // 行双击事件
    table.on('rowDouble(test)', function (obj) {
    });

  });
</script>
</body>

</html>